;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;sample-modal-dialog.lisp
;;
;;  copyright © 1987-89 Apple Computer
;;
;;this file contains source-code for a sample modal dialog
;;
;;




;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;create the dialog-object, specifying the window parameters
;;
;;  we don't want the window to be shown when it is created.  We just want to
;;  create it, so that we can later use it as a modal dialog
;;

(defvar *sample-modal-dialog* ())

(setq *sample-modal-dialog*
      (oneof *dialog*
             :window-type :double-edge-box
             :window-size #@(250 95)
             :window-position (make-point (ash (- *screen-width* 250) -1)
                                          (+ *menubar-bottom* 20))
             :window-show nil))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;add some dialog-items to the modal dialog
;;
;;  The reason we don't do specify the items as argument to ONEOF is that we
;;  want to associate the items with instance variables in the dialog.
;;  To do this, we ask the dialog to HAVE the instance variables at the same
;;  time we create the items.  If this were done inside the call to ONEOF,
;;  the have would be executed in the wrong object [in the object current
;;  when ONEOF was called, rather than in the object created by ONEOF].
;;  The calling sequence used below takes advantage of the fact that HAVE
;;  returns its second argument.  For the purpose of ADD-DIALOG-ITEMS the
;;  HAVEs are transparent.  Yet the have the effect of binding instance
;;  variables to the newly created items.
;;
;;  Because the items are bound to instance variables in the dialog, they
;;  can be easily referenced.
;;

(ask *sample-modal-dialog*
  (add-dialog-items
   (have 'my-text
         (oneof *editable-text-dialog-item*
                :dialog-item-text "Enter Expression Here"
                :dialog-item-size #@(225 16)
                :dialog-item-position #@(4 4)))
   (have 'return-string-button
         (oneof *radio-button-dialog-item*
                :dialog-item-text "Return As String"
                :dialog-item-position #@(4 30)
                :radio-button-cluster :return-choice
                :radio-button-pushed-p t))
   (have 'return-read-button
         (oneof *radio-button-dialog-item*
                :dialog-item-text "Read And Return"
                :dialog-item-position #@(4 52)
                :radio-button-cluster :return-choice))
   (have 'return-eval-result
         (oneof *radio-button-dialog-item*
                :dialog-item-text "Eval Input"
                :dialog-item-position #@(4 76)
                :radio-button-cluster :return-choice))
   (have 'ok-button
         (oneof *button-dialog-item*
                :dialog-item-text "OK"
                :dialog-item-size #@(55 16)
                :dialog-item-position #@(170 35)
                :dialog-item-action
                #'(lambda ()
                    (return-from-modal-dialog
                     (ask my-dialog
                       (let* ((choice
                               (pushed-radio-button :return-choice))
                              (my-string
                               (ask my-text (dialog-item-text))))
                         (cond
                          ((eq choice return-string-button)
                           my-string)
                          ((eq choice return-read-button)
                           (read-from-string my-string))
                          ((eq choice return-eval-result)
                           (eval (read-from-string my-string))))))))))
   (have 'cancel-button
         (oneof *button-dialog-item*
                :dialog-item-text "Cancel"
                :dialog-item-size #@(55 16)
                :dialog-item-position #@(170 62)
                :dialog-item-action
                #'(lambda () (return-from-modal-dialog :cancel))))))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;a sample call.
;;
;;  we pass MODAL-DIALOG a second argument of NIL so that it doesn't close
;;  the window upon return [it just hides the window].  In this manner, we
;;  can re-use the dialog many times without rebuilding [we just keep passing
;;  it it to MODAL-DIALOG].
;;

(modal-dialog *sample-modal-dialog* nil)